import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key);

  final GoRouter _router = GoRouter(
    initialLocation: '/home',
    routes: [
      // 定义 /home 页面
      GoRoute(
        path: '/home',
        builder: (context, state) => const HomePage(),
      ),
      // 定义 /settings 页面，包含子路由 A 和 B
      ShellRoute(
        navigatorKey: GlobalKey<NavigatorState>(), // 提供 navigatorKey 用于子路由管理
        builder: (context, state, child) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('Settings'),
            ),
            body: child, // 当前选中的子路由页面（A 或 B）
          );
        },
        routes: [
          // 父路由：Settings 页面
          GoRoute(
            path: '/settings',
            builder: (context, state) => const SettingsPage(),
            routes: [
              // 子路由：A 页面
              GoRoute(
                path: 'a',
                builder: (context, state) => const APage(),
              ),
              // 子路由：B 页面
              GoRoute(
                path: 'b',
                builder: (context, state) => const BPage(),
              ),
            ],
          ),
        ],
      ),
    ],
  );

  @override
  Widget build(BuildContext context) {
    return MaterialApp.router(
      routerConfig: _router,
      title: 'Flutter ShellRoute Example',
    );
  }
}

// Home 页面
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Home Page'),
      ),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            context.go('/settings'); // 导航到 Settings 页面
          },
          child: const Text('Go to Settings'),
        ),
      ),
    );
  }
}

// Settings 页面
class SettingsPage extends StatelessWidget {
  const SettingsPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ElevatedButton(
            onPressed: () {
              context.go('/settings/a'); // 导航到 A 页面
            },
            child: const Text('Go to A'),
          ),
          ElevatedButton(
            onPressed: () {
              context.go('/settings/b'); // 导航到 B 页面
            },
            child: const Text('Go to B'),
          ),
        ],
      ),
    );
  }
}

// A 页面
class APage extends StatelessWidget {
  const APage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Row(
        children: [
          Text('Page A'),
          ElevatedButton(
            onPressed: () {
              context.pop();
            },
            child: const Text('Go to Parent'),
          ),
        ],
      ),
    );
  }
}

// B 页面
class BPage extends StatelessWidget {
  const BPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Text('Page B'),
          ElevatedButton(
            onPressed: () {
              context.pop();
            },
            child: const Text('Go to parent'),
          ),
        ],
      ),
    );
  }
}
